#!/bin/bash
# @configure_input@

#setenv Rplain /local/software/other-R/R-2.4.0/bin/R
#setenv Rnan   /local/software/other-R/R-2.4.0-nan-optimization/bin/R
#setenv Rmod   /local/software/other-R/R-2.4.0-mod-optimization/bin/R

trap exit SIGINT SIGTERM  # quit running on CTRL-C

Rorig=/home/garvin/research/R-unmodified/R-2.1.1/bin/R
Rplain=/home/garvin/research/rcompiler-install/R-2.1.1/bin/R
Rnew=/home/garvin/research/rcompiler-install/R-new/bin/R

export PATH=@RCC_BIN_PATH@:${PATH}
export RCC_R_INCLUDE_PATH=@RCC_R_INCLUDE_PATH@
export RCC_DATA_PATH=@RCC_DATA_PATH@
export TIMECMD=/usr/bin/time

export N_TIMES=1
export ORIGINAL_INTERP=false
export MODIFIED_INTERP=false
export LATEST_INTERP=false
export COMPILED=false
export FALLBACK="1"
export RCC_OPTIONS=""

while getopts "n:omlcp:f:" flag
do
    case "$flag" in
	(n) export N_TIMES=$OPTARG ;;
	(o) export ORIGINAL_INTERP=true ;;
	(m) export MODIFIED_INTERP=true ;;
	(l) export LATEST_INTERP=true ;;
	(c) export COMPILED=true ;;
	(p)
	    case "$OPTARG" in
		(s) export FALLBACK="0 1" ;;
		(0) export FALLBACK="1" ;;
		(1) export FALLBACK="0" ;;
	    esac
	    ;;
	(f) export RCC_OPTIONS+="-f $OPTARG " ;;
    esac
done
shift $(($OPTIND - 1))

#for f1 in $POOL
#do
#    R_ALWAYS_USE_FALLBACK_ALLOC=$fi rcc-run blah blah blah
#do
#done

# for f1 in no-aggressive-CBV aggressive-CBV ; do
# for f2 in no-assume-correct-program assume-correct-program ; do

for f
do
    echo --- $f ---
    if "$ORIGINAL_INTERP"
    then
	echo "  running and timing $f with the original R interpreter 2.1.1..."
	for (( i=0 ; $i < $N_TIMES ; i=$i+1 ))
	do
	    $TIMECMD ${Rorig} --vanilla --slave < $f > ./`basename $f .r`.interpreted.orig
	done
    fi

    if "$MODIFIED_INTERP"
    then
	echo "  running and timing $f with the modified R interpreter 2.1.1..."
	for (( i=0 ; $i < $N_TIMES ; i=$i+1 ))
	do
	    $TIMECMD ${Rplain} --vanilla --slave < $f > ./`basename $f .r`.interpreted.plain
	done
    fi

    if "$LATEST_INTERP"
    then
	echo "  running and timing $f with R interpreter 2.10.1..."
	for (( i=0 ; $i < $N_TIMES ; i=$i+1 ))
	do
	    $TIMECMD ${Rnew} --vanilla --slave < $f > ./`basename $f .r`.interpreted.new
	done
    fi
    
#    if [[ $OPT == 'full' ]] ; then
#	for f1 in no-for-loop-range-deforestation for-loop-range-deforestation ; do
#	    for f2 in no-subscript-assignment subscript-assignment ; do
#		for f3 in no-strictness strictness ; do
#		    for f4 in no-call-graph call-graph ; do
#			for f5 in no-lookup-elimination lookup-elimination ; do
#			    echo "  compiling $f with rcc -f $f1 -f $f2 -f $f3 -f $f4 -f $f5 ..."
#			    rcc -f $f1 -f $f2 -f $f3 -f $f4 -f $f5 $f
#			    echo "  compiling " `basename $f .r`.c "with rcc-cc..."
#			    rcc-cc -o `basename $f .r` -g `basename $f .r`.c
#			    echo "  running " `basename $f .r` "with rcc-run..." 
#			    repeat $N_TIMES $TIMECMD rcc-run `basename $f .r` >  `basename $f .r`.compiled
#			done
#		    done
#		done
#	    done
#	done
#    else

    if "$COMPILED"
    then
	echo "  compiling $f with rcc ..."
	rcc $f $RCC_OPTIONS -o ./`basename $f .r`.c
	echo "  compiling" `basename $f .r`.c "with rcc-cc..."
	rcc-cc -O2 -o ./`basename $f .r` -g ./`basename $f .r`.c
	for p in $FALLBACK
	do
	    echo "  running" `basename $f .r` "with rcc-run (R_ALWAYS_USE_FALLBACK_ALLOC=$p)"
	    for (( i=0 ; $i < $N_TIMES ; i=$i+1 ))
	    do
		R_ALWAYS_USE_FALLBACK_ALLOC=$p $TIMECMD rcc-run `basename $f .r` >  `basename $f .r`.compiled.fallback
	    done
	done
    fi
#    fi
done
